﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Xeml.Document
{
    public class VariableTerm : Term
    {
        public VariableTerm(string termId)
            : base(termId)
        {
            _val = new ValueCollection();
        }

        public VariableTerm(VariableTerm term)
            : base(term)
        {
            _val = new ValueCollection();
            foreach (var item in term.Values)
            {
                _val.Add(new Value(item));
            }
        }

        private Xeml.Document.ValueCollection _val;
        public Xeml.Document.ValueCollection Values
        {
            get
            {
                return _val;
            }
            set
            {
                if (_val != value)
                {
                    _val = value;
                    _val.CollectionChanged += (sender, args) =>
                    {
                        OnPropertyChanged("Values");
                    };
                    OnPropertyChanged("Values");
                }
            }
        }

        /// <summary>
        /// Lists all contexts.
        /// </summary>
        /// <returns></returns>
        public List<string> ContextList()
        {
            List<string> ret = new List<string>();
            foreach (Value vb in _val)
            {
                if (!ret.Contains(vb.Context))
                    ret.Add(vb.Context);
            }
            return ret;
        }

        /// <summary>
        /// Returns all values of an given context.
        /// </summary>
        /// <param name="context">The context to filter.</param>
        /// <returns>A list of values.</returns>
        public Value ValueBy(string context)
        {
            return _val.Find(delegate(Value vb) { return vb.Context == context; });
        }
        
    }
}
